package c.x.jy.service.comm.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import c.x.jy.dao.entity.CmsArticleEntity;
import c.x.jy.dao.entity.SysUserEntity;
import c.x.jy.service.utils.mqtt.MQClientUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import c.x.jy.dao.dao.CommonDao;
import c.x.jy.service.comm.CommonService;

@CacheConfig(cacheNames = { "common" })
@Service("commonService")
public class CommonServiceImpl implements CommonService {
	@Autowired
	private CommonDao commonDao;

	@Cacheable(key = " 'common:'+ methodName")
	@Override
	public List<Map<String, Object>> areaList() {
		List<Map<String, Object>> arealist = commonDao.selectAreaList();
		List<Map<String, Object>> list = arealist.stream() 	.filter(map -> (map.get("identityCard") + "").endsWith("0000")).collect(Collectors.toList());
		arealist.removeAll(list);
		for (Map<String, Object> map : list) {
			List<Map<String, Object>> sonlist = getSonList((map.get("identityCard") + "").substring(0, 2), arealist);
			arealist.removeAll(sonlist);
			if (sonlist != null && !sonlist.isEmpty()) {
				for (Map<String, Object> map2 : sonlist) {
					List<Map<String, Object>> sonlist1 = getSonList(map2.get("identityCard").toString().substring(0, 4), 	arealist);
					if (sonlist1 != null && !sonlist1.isEmpty())
						map2.put("sonList", sonlist1);
				}
				map.put("sonList", sonlist);
			}
		}
		return list;
	}

	private List<Map<String, Object>> getSonList(String areaid, List<Map<String, Object>> list) {
		return list.stream().filter(m -> (areaid.length() == 2 ? (m.get("identityCard").toString().startsWith(areaid) && (m.get("identityCard") + "").endsWith("00")) : m.get("identityCard").toString().startsWith(areaid))).collect(Collectors.toList());
	}
	
	
	
	@Cacheable(key="'data:'+ methodName +':'+#p0 +':'+#p1",unless = "#result == null"  )
@Override
public String selectData(String sql, Long id) {
		if(id==null)return null;
	String res = commonDao.selectData(sql,id);
	return res==null?"":res;
}


	@Override
	public boolean queryonline(Long clientId) {
		if(clientId==null) return false;
		boolean b=MQClientUtil.queryDeviceStatus("xuanzhu_"+clientId);
            System.out.println(b);
		return  b;
	}
	@Cacheable(key="'data:'+ methodName +':'+#p0 +':'+#p1+#P2",unless = "#result == null"  )
	@Override
	public Map selectStatistics(SysUserEntity user) {
			return this.commonDao.selectStatistics(user.getBusinessId());
	}
	@Cacheable(key="'data:'+ methodName +':'+#p0 +':'+#p1+#P2",unless = "#result == null"  )
	@Override
	public Map<String, Object> echarts(SysUserEntity user, String begintime, String endtime) {
		Map<String,Object> res=new HashMap();
		res.put("chartZhu",this.commonDao.echartsZhu(user.getBusinessId(),begintime,endtime));
		res.put("chartBin",this.commonDao.echartsBin(user.getBusinessId(),begintime,endtime));

		return res;
	}
}
